package crafex.engine.level;

import java.util.ArrayList;
import java.util.List;

import crafex.engine.system.Command;
import crafex.engine.tools.Button;

import android.graphics.Canvas;
import android.view.MotionEvent;



public class LevelHandler {

	/**the current level*/
	private static Level level;
	
	private static boolean playing = true;
	
	/**if there are any commands pending to be used*/
	private static boolean pendingCommands = false;
	
	/**list of pending commands*/
	private static List<String> commands = new ArrayList<String>();
	/**list of all the pause GUI elements*/
	private static List<Button> pauseGUI = new ArrayList<Button>();
	
	public LevelHandler(Level defaultLevel){
		setLevel(defaultLevel);//TODO finish pauseGUI
	}
	
	public void loadPauseMenu(Button[] buttons){
		pauseGUI.clear();
		for(Button b: buttons){
			pauseGUI.add(b);
		}
	}
	
	/**
	 * gets the current level
	 * @return level
	 */
	public static Level getLevel(){
		return level;
	}
	
	public static void setLevel(Level level){
		LevelHandler.level = null;
		LevelHandler.level = level;
	}
	
	/**
	 * gets the GUI in the current level
	 * @return
	 */
	public static List<Button> getGUI(){
		return Level.getGUI();
	}
	
	public static Command getGUICommand(int index){
		return Level.getGUI().get(index).getCommand();
	}
	
	/**
	 * gets if the game is active
	 * @return
	 */
	public static boolean isPlaying() {
		return playing;
	}

	/**
	 * sets of the game is active
	 * @param playing
	 */
	public static void setPlaying(boolean playing) {
		LevelHandler.playing = playing;
	}
	
	public static void setPauseGUI(List<Button> GUI){
		pauseGUI = GUI;
	}
	
	public static List<Button> getPauseGUI(){
		return pauseGUI;
	}
	
	public static Command getPauseGUICommand(int index){
		return getPauseGUI().get(index).getCommand();
	}
	
	public static void pause(){
		LevelHandler.playing = false;
	}
	
	public static void unpause(){
		LevelHandler.playing = true;
	}

	/**
	 * passes the input into the game to prosses and to use as controll
	 * @param event
	 */
	public void updateInput(MotionEvent event){
		if(level != null){
			level.inputHandler(event);
		}
	}
	
	/**
	 * the update for the level
	 */
	public void update(){
		if(level != null){
			level.update();
		}else{
			System.out.println("your fucking up");
		}
		
		if(pendingCommands){
			useCommands();
		}
	}
	
	/**
	 * the draw for the level
	 * @param canvas to draw to
	 */
	public void draw(Canvas canvas){
		if(level != null){
			level.draw(canvas);
		}
	}
	
	/**
	 * uses all pending commands
	 */
	private void useCommands(){
		for(int c = 0; c < commands.size(); c++){
			//TODO add something to run pending commands
			
			//Crafex.console.runCommand(commands.get(c));
		}
		commands.clear();
		pendingCommands = false;
	}
	
	/**
	 * adds a command to the pending command list
	 * @param command
	 */
	public static void addCommand(String command){
		pendingCommands = true;
		commands.add(command);
	}
}